package com.laicunba.ext.service.impl;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.laicunba.constant.GloabConstant;
import com.laicunba.ext.service.JzhFtpService;
import com.laicunba.ext.service.SlaveJzhReportService;
import com.laicunba.pojo.JzhReportDO;
import com.laicunba.service.JzhReportService;
import com.laicunba.util.CacheUtil;
import com.laicunba.util.DateUtil;

@Service
public class SlaveJzhReportServiceImpl extends BaseService implements SlaveJzhReportService {
	private static final char SPLIT_CHAR = '|';
	private static final int HOUR25 = 3600 * 25;
	@Autowired
	private JzhFtpService jzhFtpService;
	@Autowired
	private JzhReportService jzhReportService;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Result reportInvestor(int maxSize) {

		Result result = new Result();
		Date date = null;
		for (int i = 0; i < 10; i++) {

			StringBuilder content = new StringBuilder();
			Map map = new HashMap();
			if (date != null) {
				map.put("beginDate", date);
			}
			List<Map> data = slaveGenericQueryDao.selectList("SLAVE_JZH_REPORT.findInverstorList", map);
			if ((data == null || data.isEmpty())&&i>0) {
				break;
			}
			for (Map row : data) {
				date = (Date) row.get("gmt_create");
				content.append(GloabConstant.JZH_MCHNT_CD).append(SPLIT_CHAR);
				content.append(row.get("mchnt_txn_ssn")).append(SPLIT_CHAR);
				content.append(row.get("cust_nm")).append(SPLIT_CHAR);
				content.append(row.get("mobile_no")).append(SPLIT_CHAR);
				content.append(row.get("age")).append(SPLIT_CHAR);
				content.append(row.get("cust_nm")).append(SPLIT_CHAR);
				content.append(row.get("certif_tp")).append(SPLIT_CHAR);
				content.append(row.get("certif_id")).append(SPLIT_CHAR);
				content.append(row.get("sex")).append(SPLIT_CHAR);
				content.append(row.get("mobile_no")).append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);
				content.append("2").append(SPLIT_CHAR);
				content.append(row.get("create_date")).append(SPLIT_CHAR);
				content.append(GloabConstant.JZH_PAY_COMPANY_ID).append(SPLIT_CHAR);
				content.append("ADD").append(SPLIT_CHAR);
				content.append("无");
				content.append("\r\n");

				JzhReportDO reportDO = new JzhReportDO();
				reportDO.setDataType("jzh_account");
				reportDO.setDataId(row.get("id").toString());
				reportDO.setId("jzh_account." + row.get("id").toString());
				reportDO.setStatus("send");
				jzhReportService.create(reportDO);
			}

			result.setFileCount(result.getFileCount() + 1);
			result.setTotalRows(result.getTotalRows() + data.size());
			System.out.println(content);
			InputStream inputStream = null;
			try {
				inputStream = new ByteArrayInputStream(content.toString().getBytes("gbk"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			long seq = CacheUtil.incr("sys.incr.jzh.report.investor." + DateUtil.formatYYYYMMDD(new Date()), HOUR25);
			String fileName = "P2P_PW10_" + DateUtil.formatYYYYMMDD(new Date()) + "_" + String.format("%04d", seq) + ".txt";
			jzhFtpService.upload(inputStream, "/check/" + fileName);

		}
		return result;
	}

	@Override
	public Result reportBorrower(int maxSize) {

		Result result = new Result();
		Date date = null;
		for (int i = 0; i < 10; i++) {

			StringBuilder content = new StringBuilder();
			Map map = new HashMap();
			if (date != null) {
				map.put("beginDate", date);
			}
			List<Map> data = slaveGenericQueryDao.selectList("SLAVE_JZH_REPORT.findBorrowerList", map);
			if ((data == null || data.isEmpty())&&i>0) {
				break;
			}
			for (Map row : data) {
				date = (Date) row.get("gmt_create");
				content.append(GloabConstant.JZH_MCHNT_CD).append(SPLIT_CHAR);
				content.append(row.get("mchnt_txn_ssn")).append(SPLIT_CHAR);
				content.append(row.get("company_name")).append(SPLIT_CHAR);
				content.append(row.get("create_date")).append(SPLIT_CHAR);
				content.append(row.get("corporate_name")).append(SPLIT_CHAR);
				content.append(row.get("pid")).append(SPLIT_CHAR);
				content.append(row.get("mobile")).append(SPLIT_CHAR);
				content.append(row.get("email")).append(SPLIT_CHAR);
				content.append(GloabConstant.JZH_PAY_COMPANY_ID).append(SPLIT_CHAR);
				content.append("ADD").append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);
				content.append(row.containsKey("org_code") ? row.get("org_code") : "").append(SPLIT_CHAR);
				content.append(row.get("company_name")).append(SPLIT_CHAR);
				content.append(row.get("mobile")).append(SPLIT_CHAR);
				content.append("无").append(SPLIT_CHAR);
				content.append("1").append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);
				content.append(row.containsKey("credit_code") ? row.get("credit_code") : "");
				content.append("\r\n");
				
				

				JzhReportDO reportDO = new JzhReportDO();
				reportDO.setDataType("borrower");
				reportDO.setDataId(row.get("id").toString());
				reportDO.setId("borrower." + row.get("id").toString());
				reportDO.setStatus("send");
				jzhReportService.create(reportDO);
				
			}

			result.setFileCount(result.getFileCount() + 1);
			result.setTotalRows(result.getTotalRows() + data.size());
			System.out.println(content);
			InputStream inputStream = null;
			try {
				inputStream = new ByteArrayInputStream(content.toString().getBytes("gbk"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			long seq = CacheUtil.incr("sys.incr.jzh.report.borrower." + DateUtil.formatYYYYMMDD(new Date()), HOUR25);
			String fileName = "P2P_PW11_" + DateUtil.formatYYYYMMDD(new Date()) + "_" + String.format("%04d", seq) + ".txt";
			jzhFtpService.upload(inputStream, "/check/" + fileName);

		}
		return result;
	}

	@Override
	public Result reportBidFreezen(int maxSize) {
		Result result = new Result();
		Date date = null;
		for (int i = 0; i < 10; i++) {

			StringBuilder content = new StringBuilder();
			Map map = new HashMap();
			if (date != null) {
				map.put("beginDate", date);
			}
			List<Map> data = slaveGenericQueryDao.selectList("SLAVE_JZH_REPORT.findBidOrderFreezenList", map);
			if ((data == null || data.isEmpty())&&i>0) {
				break;
			}
			for (Map row : data) {
				date = (Date) row.get("gmt_create");
				content.append(GloabConstant.JZH_MCHNT_CD).append(SPLIT_CHAR);
				content.append(row.get("mchnt_txn_ssn")).append(SPLIT_CHAR);
				content.append(row.get("create_date")).append(SPLIT_CHAR);
				content.append("PWDJ").append(SPLIT_CHAR);
				content.append(row.get("target_code")).append(SPLIT_CHAR);
				content.append(row.get("contract_no")).append(SPLIT_CHAR);
				content.append(row.get("out_cust_no")).append(SPLIT_CHAR);
				content.append(row.get("out_true_name")).append(SPLIT_CHAR);

				content.append(row.get("cent_buy_amount")).append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);
				content.append(row.get("in_cust_no")).append(SPLIT_CHAR);
				content.append(row.get("in_true_name")).append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);
				content.append("0").append(SPLIT_CHAR);

				content.append(GloabConstant.JZH_PAY_COMPANY_ID);
				content.append("\r\n");
				
				

				JzhReportDO reportDO = new JzhReportDO();
				reportDO.setDataType("bid_freezen");
				reportDO.setDataId(row.get("id").toString());
				reportDO.setId("bid_freezen." + row.get("id").toString());
				reportDO.setStatus("send");
				jzhReportService.create(reportDO);
				
			}

			result.setFileCount(result.getFileCount() + 1);
			result.setTotalRows(result.getTotalRows() + data.size());
			System.out.println(content);
			InputStream inputStream = null;
			try {
				inputStream = new ByteArrayInputStream(content.toString().getBytes("gbk"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			long seq = CacheUtil.incr("sys.incr.jzh.report.pwjy." + DateUtil.formatYYYYMMDD(new Date()), HOUR25);
			String fileName = "P2P_PWJY_" + DateUtil.formatYYYYMMDD(new Date()) + "_" + String.format("%04d", seq) + ".txt";
			jzhFtpService.upload(inputStream, "/check/" + fileName);

		}
		return result;
	}

	@Override
	public Result reportRepayment(int maxSize) {
		Result result = new Result();
		Date date = null;
		for (int i = 0; i < 10; i++) {

			StringBuilder content = new StringBuilder();
			Map map = new HashMap();
			if (date != null) {
				map.put("beginDate", date);
			}
			List<Map> data = slaveGenericQueryDao.selectList("SLAVE_JZH_REPORT.findRepaymentList", map);
			if ((data == null || data.isEmpty())&&i>0) {
				break;
			}
			for (Map row : data) {
				date = (Date) row.get("gmt_create");
				content.append(GloabConstant.JZH_MCHNT_CD).append(SPLIT_CHAR);
				content.append(row.get("mchnt_txn_ssn")).append(SPLIT_CHAR);
				content.append(row.get("create_date")).append(SPLIT_CHAR);
				content.append("PWDZ").append(SPLIT_CHAR);
				content.append(row.get("code")).append(SPLIT_CHAR);
				content.append(row.get("bid_contract_no")).append(SPLIT_CHAR);
				content.append(row.get("out_cust_no")).append(SPLIT_CHAR);
				content.append(row.get("out_true_name")).append(SPLIT_CHAR);

				content.append(row.get("cent_amt")).append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);
				content.append(row.get("in_cust_no")).append(SPLIT_CHAR);
				content.append(row.get("in_true_name")).append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);
				content.append("3").append(SPLIT_CHAR);

				content.append(GloabConstant.JZH_PAY_COMPANY_ID);
				content.append("\r\n");
				
				
				JzhReportDO reportDO = new JzhReportDO();
				reportDO.setDataType("jzh_repayment");
				reportDO.setDataId(row.get("id").toString());
				reportDO.setId("jzh_repayment." + row.get("id").toString());
				reportDO.setStatus("send");
				jzhReportService.create(reportDO);
				
			}

			result.setFileCount(result.getFileCount() + 1);
			result.setTotalRows(result.getTotalRows() + data.size());
			System.out.println(content);
			InputStream inputStream = null;
			try {
				inputStream = new ByteArrayInputStream(content.toString().getBytes("gbk"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			long seq = CacheUtil.incr("sys.incr.jzh.report.pwjy." + DateUtil.formatYYYYMMDD(new Date()), HOUR25);
			String fileName = "P2P_PWJY_" + DateUtil.formatYYYYMMDD(new Date()) + "_" + String.format("%04d", seq) + ".txt";
			jzhFtpService.upload(inputStream, "/check/" + fileName);

		}
		return result;
	}

	@Override
	public Result reportBid(int maxSize) {
		Result result = new Result();
		Date date = null;
		for (int i = 0; i < 10; i++) {

			StringBuilder content = new StringBuilder();
			Map map = new HashMap();
			if (date != null) {
				map.put("beginDate", date);
			}
			List<Map> data = slaveGenericQueryDao.selectList("SLAVE_JZH_REPORT.findBidOrderFangKuanList", map);
			if ((data == null || data.isEmpty())&&i>0) {
				break;
			}
			for (Map row : data) {
				date = (Date) row.get("gmt_create");
				content.append(GloabConstant.JZH_MCHNT_CD).append(SPLIT_CHAR);
				content.append(row.get("certificate_id")).append(SPLIT_CHAR);
				content.append(row.get("create_date")).append(SPLIT_CHAR);
				content.append("PWDZ").append(SPLIT_CHAR);
				content.append(row.get("code")).append(SPLIT_CHAR);
				content.append(row.get("contract_no")).append(SPLIT_CHAR);
				content.append(row.get("out_cust_no")).append(SPLIT_CHAR);
				content.append(row.get("out_true_name")).append(SPLIT_CHAR);

				content.append(row.get("cent_buy_amount")).append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);
				content.append(row.get("in_cust_no")).append(SPLIT_CHAR);
				content.append(row.get("in_true_name")).append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);
				content.append("1").append(SPLIT_CHAR);

				content.append(GloabConstant.JZH_PAY_COMPANY_ID);
				content.append("\r\n");
				
				JzhReportDO reportDO = new JzhReportDO();
				reportDO.setDataType("jzh_bid_order");
				reportDO.setDataId(row.get("id").toString());
				reportDO.setId("jzh_bid_order." + row.get("id").toString());
				reportDO.setStatus("send");
				jzhReportService.create(reportDO);
				
			}

			result.setFileCount(result.getFileCount() + 1);
			result.setTotalRows(result.getTotalRows() + data.size());
			System.out.println(content);
			InputStream inputStream = null;
			try {
				inputStream = new ByteArrayInputStream(content.toString().getBytes("gbk"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			long seq = CacheUtil.incr("sys.incr.jzh.report.pwjy." + DateUtil.formatYYYYMMDD(new Date()), HOUR25);
			String fileName = "P2P_PWJY_" + DateUtil.formatYYYYMMDD(new Date()) + "_" + String.format("%04d", seq) + ".txt";
			jzhFtpService.upload(inputStream, "/check/" + fileName);

		}
		return result;
	}

	@Override
	public Result reportFinancingTarget(int maxSize) {
		Result result = new Result();
		Date date = null;
		for (int i = 0; i < 10; i++) {

			StringBuilder content = new StringBuilder();
			Map map = new HashMap();
			if (date != null) {
				map.put("beginDate", date);
			}
			List<Map> data = slaveGenericQueryDao.selectList("SLAVE_JZH_REPORT.findFinancingTargetList", map);
			if ((data == null || data.isEmpty())&&i>0) {
				break;
			}
			for (Map row : data) {
				date = (Date) row.get("gmt_create");
				content.append(GloabConstant.JZH_MCHNT_CD).append(SPLIT_CHAR);
				content.append(row.get("code")).append(SPLIT_CHAR);
				content.append(row.get("code")).append(SPLIT_CHAR);
				content.append("1").append(SPLIT_CHAR);
				content.append(row.get("name")).append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);
				content.append("经营").append(SPLIT_CHAR);
				content.append(row.get("cent_collect_amount")).append(SPLIT_CHAR);

				content.append(row.get("annualized_rates")).append(SPLIT_CHAR);
				content.append(row.get("name")).append(SPLIT_CHAR);
				content.append("0").append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);// 借款期限

				content.append(row.get("begin_date_str")).append(SPLIT_CHAR);
				content.append("100").append(SPLIT_CHAR);
				content.append("999").append(SPLIT_CHAR);
				content.append("1000000000").append(SPLIT_CHAR);
				content.append(row.get("login_id")).append(SPLIT_CHAR);
				content.append(row.get("login_id")).append(SPLIT_CHAR);
				content.append("暂无").append(SPLIT_CHAR);

				content.append("0").append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);
				content.append("1").append(SPLIT_CHAR);
				content.append("").append(SPLIT_CHAR);

				content.append(GloabConstant.JZH_PAY_COMPANY_ID);
				content.append("\r\n");
				
				
				JzhReportDO reportDO = new JzhReportDO();
				reportDO.setDataType("financing_target");
				reportDO.setDataId(row.get("id").toString());
				reportDO.setId("financing_target." + row.get("id").toString());
				reportDO.setStatus("send");
				jzhReportService.create(reportDO);
				
			}

			result.setFileCount(result.getFileCount() + 1);
			result.setTotalRows(result.getTotalRows() + data.size());
			System.out.println(content);
			InputStream inputStream = null;
			try {
				inputStream = new ByteArrayInputStream(content.toString().getBytes("gbk"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			long seq = CacheUtil.incr("sys.incr.jzh.report.pwxm." + DateUtil.formatYYYYMMDD(new Date()), HOUR25);
			String fileName = "P2P_PWXM_" + DateUtil.formatYYYYMMDD(new Date()) + "_" + String.format("%04d", seq) + ".txt";
			jzhFtpService.upload(inputStream, "/check/" + fileName);

		}
		return result;
	}

}
